<script setup name="sysCode">

import {Pane, Splitpanes} from "splitpanes";
import "splitpanes/dist/splitpanes.css"
import {delSysCode, querySysCodeAll, querySysCodeValueByCodeType} from "@/api/admin/SysCode.js";
import app from "@/store/modules/app.js";
import AddSysCode from "@/views/admin/sysCode/addSysCode.vue";
import modal from "@/plugins/modal.js";
import {handleTree} from "@/utils/ruoyi.js";

const appStore = app()
/**
 * sysCode
 *
 */
const sysCodeData = ref([])

const query = ref("")

const page = ref({
  total: 0,
  pageSize: 20,
  pageNum: 1,
  queryValue: ''
})
const querySysCode = () => {
  querySysCodeAll(page.value)
      .then(res => {
        sysCodeData.value = res.rows;
        page.value.total = res.total;
      })
}

querySysCode();
const del = (row) => {
  delSysCode(row.codeType)
      .then(res => {
        modal.msgSuccess("删除成功")
        querySysCode();
      })

}

/**
 * sysCodeValueData
 *
 */
const sysCodeValueData = ref([])

let pageSysCodeValueData = []

const sysCodeValuePage = ref({
  pageSize: 10,
  pageNum: 1,
  total: 0
})

const sysCodeClick = (row) => {
  querySysCodeValueByCodeType(row.codeType)
      .then(res => {
        sysCodeValueData.value = res
      })
}

const limit = () => {
  let currentPage = sysCodeValuePage.value.pageSize * (sysCodeValuePage.value.pageNum - 1)
  let endPage = sysCodeValuePage.value.pageNum * sysCodeValuePage.value.pageSize
  sysCodeValueData.value = pageSysCodeValueData.slice(currentPage, endPage);
}
const querySysCodeValue = () => {
  limit();
}
const loadTree = (row, treeNode, resolve) => {
  console.log(row)
  console.log(treeNode)
  console.log(resolve)
}

const selectChangeEvent = () => {

}

const open = ref(false)
const title = ref("新增")
const form = ref({})
const state = ref(0);

const handelAdd = () => {
  form.value = {}
  open.value = true
  title.value = "新增"
  state.value = 0
}


const handelEdit = (row) => {
  form.value = row
  open.value = true
  title.value = "修改"
  state.value = 1
}
</script>

<template>
  <div class="app-container">
    <el-row :gutter="10">
      <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
        <pane size="30">
          <el-card style="height: calc(100vh - 125px)">
            <div style="margin: 3px">
              <el-input
                  v-model="page.queryValue"
                  style="width: 340px;margin: 0 3px 0 3px"
                  placeholder="请输入代码值或者名称"

              ></el-input>
              <el-button type="primary" @click="querySysCode">搜索
              </el-button>
              <el-row style="margin-top: 5px;margin-bottom: 5px;">
                <el-button type="primary" @click="handelAdd">
                  新增
                </el-button>
              </el-row>
            </div>
            <el-table
                :data="sysCodeData"
                style="width: 100%; margin-bottom: 20px"
                :max-height="600"
                @row-click="sysCodeClick"
            >
              <el-table-column prop="codeType" label="代码类型"
                               :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="typeName" label="代码名称"
                               :show-overflow-tooltip="true"></el-table-column>
              <el-table-column label="操作" min-width="80">
                <template #default="scope">

                  <el-button link type="primary" size="small" @click="handelEdit(scope.row)">修改
                  </el-button>
                  <el-popconfirm title="确定要删除吗?"
                                 @confirm="del(scope.row)"
                  >
                    <template #reference>
                      <el-button link type="primary" size="small">删除
                      </el-button>
                    </template>
                  </el-popconfirm>

                </template>
              </el-table-column>
            </el-table>
            <el-pagination
                size="small"
                background
                v-model:page-size="page.pageSize"
                v-model:current-page="page.pageNum"
                layout="prev, pager, next"
                :total="page.total"
                class="mt-4"
                @current-change="querySysCode"
            />
          </el-card>
        </pane>
        <pane size="70">
          <el-col>
            <el-card style="height: calc(100vh - 125px)">
              <vxe-table
                  ref="tableRef"
                  max-height="800"

                  :column-config="{resizable: true}"
                  :tree-config="{transform: true, rowField: 'codeValue', parentField: 'subCodeValue'}"
                  :data="sysCodeValueData"
                  :checkbox-config="{labelField: 'codeValue', highlight: true,range:true}"
                  @checkbox-change="selectChangeEvent">
                <vxe-column type="checkbox" title="ID" width="280" tree-node></vxe-column>
                <vxe-column field="codeValue" title="代码值"></vxe-column>
                <vxe-column field="codeName" title="代码名称1"></vxe-column>
                <vxe-column field="codeName2" title="代码名称2"></vxe-column>
                <vxe-column field="codeName3" title="代码名称3"></vxe-column>
              </vxe-table>

            </el-card>
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
    <add-sys-code v-model:open="open" :title="title" :form="form" :state="state"
                  @refresh-table="querySysCode"></add-sys-code>
  </div>
</template>

<style scoped lang="scss">

</style>